<template>
  <div class="pieChart">
    <div id="carbpiechart" class="echart"></div>
  </div>
</template>

<script>
import echarts from "@/utils/lib/echarts";
import { PieChart } from "echarts/charts";
echarts.use([PieChart]);
import colorConst from "@/utils/custom-color.js";
export default {
  name: "pieChartofre",
  props: {
    // 饼图数据
    pieData: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    pieData: {
      handler(val) {
        if (val) {
          this.pieCharts(this.pieData);
        }
      },
    },
  },
  beforeDestroy() {
    this.chartDes();
  },
  methods: {
    chartDes() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    pieCharts(pieData) {
      let option = {
        color: colorConst.carbonsinkcolor,
        tooltip: {
          trigger: "item",
        },
        legend: {
          type: "scroll",
          orient: "vertical",
          top: "20%",
          padding: 0,
          right: "1%",
          icon: "circle",
          itemGap: 3,
          textStyle: {
            color: "#8C8C8C",
            fontFamily: "PingFang-SC-Regular, PingFang-SC",
            fontWeight: 400,
            lineHeight: 17,
          },
        },
        series: [
          {
            name: "碳汇渠道分析",
            type: "pie",
            radius: ["60%", "90%"],
            avoidLabelOverlap: false,
            itemStyle: {
              // borderRadius: 10,
            },
            label: {
              show: false,
              position: "center",
              color: "#1890FF",
              formatter: function (params) {
                return params.value; // 中间显示数据
              },
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "38",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: pieData,
          },
        ],
      };
      this.initChart("carbpiechart", option);
    },
    initChart(id, options) {
      this.chart = echarts.init(document.getElementById(id));
      this.chart.setOption(options);
      window.addEventListener("resize", () => {
        if (this.chart) {
          this.chart.resize();
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.pieChart {
  .echart {
    width: 100%;
    height: 230px;
  }
}
</style>
